package com.aliang.shopping.service.impl;


import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.contact.SqlColumnConstant;
import com.aliang.shopping.mapper.GoodsTypeMapper;
import com.aliang.shopping.model.converter.CommonConverter;
import com.aliang.shopping.model.converter.GoodsTypeConverter;
import com.aliang.shopping.model.dto.common.PageDTO;
import com.aliang.shopping.model.po.GoodsType;
import com.aliang.shopping.model.vo.goods.GoodsTypeVO;
import com.aliang.shopping.service.GoodsTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ShiBaLover
 * @description 针对表【goodsType(商品分类表)】的数据库操作Service实现
 * @createDate 2024-10-22 11:53:05
 */
@Service
@AllArgsConstructor
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsType>
        implements GoodsTypeService {
    private final CommonConverter<GoodsTypeVO> commonConverter = new CommonConverter<>();
    private final GoodsTypeConverter goodsTypeConverter;


    @Override
    public PageResult<GoodsTypeVO> selectPage(PageDTO pageDTO) {
        //查询数据
        IPage<GoodsTypeVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        //查询父类分页数据
        IPage<GoodsTypeVO> mapperPage = this.baseMapper.getPageWithParent(page, pageDTO);
        //转换数据
        PageResult<GoodsTypeVO> pageResult = commonConverter.toPageResult(mapperPage);
        //获取父类数据
        List<GoodsTypeVO> rows = pageResult.getRows();
        if (!rows.isEmpty()) {
            // 获取父类Id
            List<Long> parentIds = rows.stream().map(GoodsTypeVO::getId).collect(Collectors.toList());
            //根据父类Id获取子类数据
            List<GoodsType> childrenTypes = this.baseMapper.selectList(new LambdaQueryWrapper<GoodsType>()
                    .in(GoodsType::getParentId, parentIds) // 根据父类Id查询
                    .eq(GoodsType::getStatus, SqlColumnConstant.STATUS_OK)// 状态为正常
                    .orderByAsc(GoodsType::getOrderNum) // 按排序字段升序
            );
            if (!childrenTypes.isEmpty()) {
                //子类数据不为空，转换为vo
                List<GoodsTypeVO> voList = goodsTypeConverter.toVoList(childrenTypes);
                //转换成map
                Map<Long, List<GoodsTypeVO>> collect = voList.stream().collect(Collectors.groupingBy(GoodsTypeVO::getParentId));
                //填充子类数据
                rows.forEach(goodsTypeVO -> goodsTypeVO.setChildren(collect.getOrDefault(goodsTypeVO.getId(), Collections.emptyList()))
                );
            }
            //重新设置数据
            pageResult.setRows(rows);
        }
        return pageResult;
    }
}




